﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace WindowsWrapperEditor
{
    public class NodeTreeView : TreeView
    {
        private HtmlNode baseNode;
        public HtmlNode BaseNode
        {
            get { return baseNode; }
            set
            {
                baseNode = value;
                if (baseNode != null)
                    PopulateTreeview();
            }
        }
        public void PopulateTreeview()
        {
            this.Nodes.Clear();
            var header = baseNode.NodeType == HtmlNodeType.Document ? "DocumentElement" : baseNode.OriginalName;
            //We create the base node here, that way as new nodes are added we can animate them ;)
            TreeNode document = new TreeNode { Text = header, Tag = baseNode, };
            this.Nodes.Add(document);
            PopulateItem(baseNode, document);
        }

        private void PopulateItem(HtmlNode htmlNode, TreeNode item)
        {
            TreeNode attributes = new TreeNode { Text = "Attributes" };
            foreach (var att in htmlNode.Attributes)
                attributes.Nodes.Add(new TreeNode
                {
                    Text = string.Format("{0} = {1}", att.OriginalName, att.Value),
                    Tag = att
                });
            //If we don't have any attributes, don't add the node
            if (attributes.Nodes.Count > 0)
                item.Nodes.Add(attributes);

            //Create the Elements Collection
            TreeNode elements = new TreeNode { Text = "Elements", Tag = htmlNode };
            foreach (var node in htmlNode.ChildNodes)
            {
                //If there are no attributes, no need to add a node inbetween the parent in the treeview
                if (attributes.Nodes.Count > 0)
                    elements.Nodes.Add(BuildTree(node));
                else
                    item.Nodes.Add(BuildTree(node));
            }

            //If there are no nodes in the elements collection, don't add to the parent 
            if (elements.Nodes.Count > 0)
                item.Nodes.Add(elements);
        }

        private TreeNode BuildTree(HtmlNode htmlNode)
        {
            //Create the main treeview node for this htmlnode
            TreeNode item = new TreeNode { Tag = htmlNode }; //preserve reference to _html node for databinding

            //if we have psuedo element, show it's text
            if (htmlNode.NodeType == HtmlNodeType.Text || htmlNode.NodeType == HtmlNodeType.Comment)
                item.Text = string.Format("<{0}> = {1}", htmlNode.OriginalName, htmlNode.InnerText.Trim());
            else
                item.Text = string.Format("<{0}>", htmlNode.OriginalName);

            //Create Attribute collection
            PopulateItem(htmlNode, item);

            return item;
        }
    }
}